package com.dingwei.superiordiary.adapter.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.app.MyApplication;
import com.dingwei.superiordiary.bean.CommodityDetailsBean;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.ScreenUtils;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lihao on 2018/1/8.
 */

public class IntegralMallAdapter extends RecyclerArrayAdapter<CommodityDetailsBean.DataBean> {

    public interface ExchangeInterFace {
        void exchange(int position);
    }

    private ExchangeInterFace exchangeInterFace;

    public void xxInterfaceListener(ExchangeInterFace listener) {
        exchangeInterFace = listener;

    }


    public IntegralMallAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new IntegralMallHolder(parent);
    }

    class IntegralMallHolder extends BaseViewHolder<CommodityDetailsBean.DataBean> {

        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_point)
        TextView tvPoint;
        @Bind(R.id.tv_Already)
        TextView tvAlready;
        @Bind(R.id.btn_exchange)
        SuperButton btnExchange;
        @Bind(R.id.img)
        ImageView img;

        public IntegralMallHolder(ViewGroup parent) {
            super(parent, R.layout.item_integral_mall);
            ButterKnife.bind(this, itemView);

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((ScreenUtils.getScreenWidth(BaseActivity.context) - 48) / 2, (ScreenUtils.getScreenWidth(BaseActivity.context) - 48) / 2);
            img.setLayoutParams(lp);
        }

        @Override
        public void setData(CommodityDetailsBean.DataBean data) {
            super.setData(data);

            tvName.setText(data.name);
            tvPoint.setText(data.integral+"次元币");
            tvAlready.setText("已兑换" + data.sales_volume + "件");
            Glide.with(BaseActivity.context)
                    .load(data.icon)
                    .apply(MyApplication.options_no_transform)
                    .into(img);

            btnExchange.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (exchangeInterFace!=null){
                        exchangeInterFace.exchange(getDataPosition());
                    }
                }
            });
        }
    }
}
